package com.p.lib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.heartware.sh.commonlib.R;
import com.p.lib.util.DensityUtil;

public class ProgressDialog extends Dialog {

    final static String TAG = "ProgressDialog";
    private TextView tv_message;
    private String message;
    private long number;
    private ProgressBar progressBar;
    Context context;

    private boolean isTouchOutside;

    private OnLoadingListener onLoadingListener;

    public interface OnLoadingListener{

        void onShow();

        void onDismiss(boolean isClickBack);

    }

    public ProgressDialog(Context context) {
        super(context, R.style.Dialog);
//        super(context);
        this.context = context;
    }

    public ProgressDialog(Context context, String message) {
        super(context, R.style.Dialog);
//        super(context);
        this.context = context;
        this.message = message;
    }

    public ProgressDialog(Context context, String message, boolean isTouchOutside, OnLoadingListener onLoadingListener) {
        super(context, R.style.Dialog);
//        super(context);
        this.context = context;
        this.message = message;

        this.isTouchOutside = isTouchOutside;
        this.onLoadingListener = onLoadingListener;
    }

    public ProgressDialog(Context context, int theme){
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.v_progress_dialog);
        progressBar = findViewById(R.id.pb);
        tv_message = (TextView)findViewById(R.id.tv_message);
        if(message != null){
            tv_message.setText(message);
        }

        setCanceledOnTouchOutside(isTouchOutside);

        setDialogSize();
    }

    public void show(String message) {
        try{
//    		if(progressBar!= null){
//                progressBar.start();
//            }
//            super.show();
            show();
            this.message = message;
            if(tv_message != null && message != null){
                tv_message.setText(message);
            }
    	}catch(Exception e){
    		Log.e(TAG, e.getMessage());
    	}
    }
    
    public void show(int msgResId){
        show(getContext().getString(msgResId));
    }

    @Override
    public void show() {
//        if(progressBar!= null){
//            progressBar.start();
//        }
        if(onLoadingListener != null){
            onLoadingListener.onShow();
        }
        super.show();
    }

    public ProgressDialog setMessage(String message){
        this.message = message;
        if(tv_message != null && message != null){
            tv_message.setText(message);
        }
        return this;
    }

    public ProgressDialog setMessage(int resId){
        this.message = getContext().getString(resId);
        if(tv_message != null && message != null){
            tv_message.setText(message);
        }
        return this;
    }

    public void setNumber(long number) {
        this.number = number;
    }
    public long getNumber() {
        return number;
    }

//    @Override
//    public void hide() {
////        if(progressBar!= null){
////            progressBar.stop();
////        }
//        super.hide();
//        dismiss();
//    }

    @Override
    public void dismiss() {
        if(onLoadingListener != null){
            onLoadingListener.onDismiss(isClickBack);
        }
        super.dismiss();
    }


    private boolean isClickBack = false;

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK){

//                    if(onLoadingListener != null){
//                        onLoadingListener.onDismiss(true);
//                    }

            isClickBack = true;

            this.dismiss();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void setDialogSize(){
        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);

//        WindowManager w = this..getWindowManager();
//        DisplayMetrics dm= new DisplayMetrics();
//        w.getDefaultDisplay().getMetrics(dm);
//
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        if(dm.widthPixels < dm.heightPixels){
//            width = (int) (dm.widthPixels * 0.75);
//            height = (int) (dm.heightPixels * 0.25);
//        }
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DensityUtil.screenWidth * 0.75);
        lp.height = (int) (DensityUtil.screenHeight * 0.25);
        lp.alpha = 0.85f;
        dialogWindow.setAttributes(lp);
    }
}
